<?php
namespace Admin\Controller;
use Think\Controller;
/*
 * 社区党建————队伍建设管理
 */
class TeamController extends CommonController
{
    public function index()
    {

        $table = 'com_team';
        $field = 'id,title,content,read_num,praise,update_time';
        $where = 'deletes = 1';

        if(!isset($_GET['p'])) {
            $_GET['p'] = 1;
        }


        // 实例化User对象
        $User = M($table);
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $list = $User->field($field)->where($where)->order('id desc')->page($_GET['p'].',10')->select();
        // 赋值分页输出
        $this->assign('team',$list);

        // 查询满足要求的总记录数
        $count      = $User->where($where)->count();

        // 实例化分页类 传入总记录数和每页显示的记录数
        $Page       = new \Think\Page($count,10);

        $Page -> setConfig('header','共%TOTAL_ROW%条');
        $Page -> setConfig('first','首页');
        $Page -> setConfig('last','共%TOTAL_PAGE%页');
        $Page -> setConfig('prev','上一页');
        $Page -> setConfig('next','下一页');
        $Page -> setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');

        $show       = $Page->show();

        // 分页显示输出
        $this->assign('page',$show);

        $this->display(); // 输出模板


//        $table = 'com_team';
//        $field = 'id,title,content,read_num,praise,update_time';
//        $where = 'deletes = 1';
//        $team = D('Street') -> dataInfo($table,$field,$where);
//        $this->assign('team',$team);
//        $this -> display();
    }
    //队伍建设添加
    public function add()
    {
        $data = $_POST;
        if(!empty($data)){
            $team = M('com_team');
            $data['create_time'] = date("Y-m-d H:i:s",time());
            $data['update_time'] = date("Y-m-d H:i:s",time());
            if($data){
                $add = $team  -> add($data);
                if($add){
                    $info['title'] ="恭喜您！";
                    $info['text'] = "添加成功！";
                    $info['type'] = "success";
                }else{
                    $info['title'] ="对不起！";
                    $info['text'] = "添加失败！";
                    $info['type'] = "error";
                }
            }else{
                $info['title'] ="对不起！";
                $info['text'] = "添加失败，请重新填写信息！";
                $info['type'] = "error";
            }
            $this -> ajaxReturn($info);
        }else{
            $c_table = 'community';
            $c_field = 'id,name';
            $where = 'deletes = 1';
            $comm = D('Street') -> datainfo($c_table,$c_field,$where);
            $this->assign('comm',$comm);
            $this->display();
        }
    }
    //队伍建设修改
    public function upd()
    {
        $id = $_GET['id'];
        $data = $_POST;
        if(!empty($data)){
            $team = M('com_team');
            $data['update_time'] = date("Y-m-d H:i:s",time());
            if($data){
                $add = $team ->where('id = '.$data['id'])-> field('title,content,update_time') -> save($data);
                if($add){
                    $info['title'] ="恭喜您！";
                    $info['text'] = "修改成功！";
                    $info['type'] = "success";
                }else{
                    $info['title'] ="对不起！";
                    $info['text'] = "修改失败！";
                    $info['type'] = "error";
                }
            }else{
                $info['title'] ="对不起！";
                $info['text'] = "修改失败，请重新填写信息！";
                $info['type'] = "error";
            }
            $this -> ajaxReturn($info);
        }else{
            //社区显示
            $c_table = 'community';
            $c_field = 'id,name';
            $c_where = 'deletes = 1';
            $comm = D('Street') -> datainfo($c_table,$c_field,$c_where);
            //文章表显示
            $table = 'com_team as t';
            $field = 't.id,t.com_id,t.title,t.content,c.name';
            $where = 't.id = '.$id;
            $join = 'hd_community as c on t.com_id = c.id';
            $team = M($table) ->join($join) -> field($field) -> where($where) -> find();
            $this->assign('team',$team);
            $this->assign('comm',$comm);
            $this->display();
        }
    }
}